<h1>Database: '<?=$db->name?>'</h1>
<p>
	<?=HTML::a('Back to all databases', 'db', 'list')?>
	<?=HTML::a('View reports', 'db', 'show_reports', $db->id)?>
</p>

<?=HTML::form('newTable', 'db', 'create_table', $db->id)?>
	<div class="dbTable new">
		<h2>New Table</h2>
		<?=HTML::labeledInput('Name', 'tableName')?>
		<?=HTML::submit('Create')?>
	</div>
</form>

<?php foreach ( $db->tables as $table ): ?>
	<div class="dbTable" id="tbl_<?=$table->id?>">
		<h2><?=$table->name?></h2>
		
		<?=HTML::form('delTable'.$table->id, 'table', 'delete', $table->id)?>
			<div class="deleteButton"><?=HTML::submit('Delete')?></div>
		</form>	
		<?=HTML::a('View data', 'table', 'show', $table->id)?>
		
		<?=HTML::form('updateTable', 'table', 'update', $table->id)?>
			<table>
				<tr>
					<th>Field Type</th>
					<th>Field Name</th>
					<th>After Field</th>
				</tr>
								
				<tr class="new">
					<td>
						<?=HTML::select('newFieldType', array_keys(Types::$sql))?>
					</td>
					<td><?=HTML::input('newFieldName')?></td>
					<td><?=HTML::select('newFieldAfter', $table->afterFields(), array('useID' => true))?></td>
				</tr>
				
				<? /* Work through our fields */ ?>
				<? foreach ( $table->fields as $field ): $id = $field->id; ?>
					<tr>
						<td>
							<? if ( $field->isVirtual ): ?>
								<?=HTML::hiddenInput("fields[$id][type]", $field->type)?>
								<?=$field->type?>
							<? else: ?>
								<?=HTML::select("fields[$id][type]", array_keys(Types::$sql), array('selected' => $field->type))?>
							<? endif; ?>
						</td>
						<td>
							<? if ( $field->isVirtual ): ?>
								<?=$field->displayName?>
								<?=HTML::hiddenInput("fields[$id][name]", $field->name)?>
							<? else: ?>
								<?=HTML::input("fields[$id][name]", array('value' => $field->displayName))?>
							<? endif; ?>
						</td>
						<td><?=HTML::select("fields[$id][after]", $table->afterFields($field), array('useID' => true))?></td>
						<td>
							<?=HTML::labeledInput('Delete?', "fields[$id][delete]", array('type' => "checkbox", 'class' => "delete", 'id' => "delField_".$table->id."_$id"))?>
						</td>
					</tr>
				<? endforeach; ?>
								
			</table>
			<p><?=HTML::a('New Virtual Field', 'table', 'new_vfield', $table->id)?></p>
			<p><?=HTML::submit('Update')?></p>
		</form>		
		<?=HTML::form('rename'.$table->id, 'table', 'rename', $table->id)?>
			<p><?=HTML::input('rename')?><?=HTML::submit('Rename')?></p>
		</form>
	</div>
<? endforeach; ?>
